package cn.wit.summit.chwan.ui.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.models.dto.ThumbsDataBean;
import cn.wit.summit.chwan.models.response.ResponseBase;
import cn.wit.summit.chwan.net.BuildRequestUtils;
import cn.wit.summit.chwan.net.NetCallBack;
import cn.wit.summit.chwan.net.okhttplib.OkHttpUtil;
import cn.wit.summit.chwan.retrofit.ApiUtil;
import cn.wit.summit.chwan.retrofit.MyCallback;
import cn.wit.summit.chwan.retrofit.RetrofitManager;
import cn.wit.summit.chwan.ui.base.MyActivity;
import cn.wit.summit.chwan.ui.base.MyFragmentActivity;
import cn.wit.summit.chwan.ui.fragment.NoticeCommentFragment;
import cn.wit.summit.chwan.utils.AccountUtil;
import cn.wit.summit.chwan.utils.DateUtils;
import cn.wit.summit.chwan.utils.LogUtils;
import cn.wit.summit.chwan.utils.StringToUnicodeUtil;
import cn.wit.summit.chwan.widgets.refreshLayout.SmartRefreshLayout;
import cn.wit.summit.chwan.widgets.refreshLayout.api.RefreshLayout;
import cn.wit.summit.chwan.widgets.refreshLayout.listener.OnLoadMoreListener;
import cn.wit.summit.chwan.widgets.refreshLayout.listener.OnRefreshListener;
import retrofit2.Retrofit;

import static android.media.CamcorderProfile.get;

/**
 * Created by pengsk on 2018/4/22.
 */

public class ThumbsActivity extends MyActivity {

    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @Bind(R.id.root_content_null_ll)
    FrameLayout root_content_null_ll;
    @Bind(R.id.retryText)
    TextView retryText;
    private int pn = 1;
    LayoutInflater inflater;
    List<MainBean> datas;

    private RetrofitManager retrofitManager;
    private Retrofit retrofit;
    private ApiUtil api;

    class MainBean {
        public Object object;
        public int type;

        public MainBean(Object object, int type) {
            this.object = object;
            this.type = type;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_thumbs_layout);
        ButterKnife.bind(this);

        retrofitManager = RetrofitManager.getInstance(mContext);
        retrofit = retrofitManager.getRetrofit();
        api = retrofit.create(ApiUtil.class);

        datas = new ArrayList<>();
        setBarTitleTx("点赞");
        inflater = LayoutInflater.from(this);
        refreshLayout.autoLoadMore();
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                getData();
            }
        });
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pn = 1;
                getData();
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new MYThumbsAdapter();

        recyclerView.setAdapter(adapter);
        getData();
    }

    private MYThumbsAdapter adapter;

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void getData() {
        api.getThumbsData(AccountUtil.getInstance().getUid(), AccountUtil.getInstance().getToken(), pn)
                .enqueue(new MyCallback<ResponseBase<List<ThumbsDataBean>>>() {
                    @Override
                    public void onSuccess(ResponseBase<List<ThumbsDataBean>> model) {
                        List<ThumbsDataBean> data = model.getData_info();
                        if (data == null) {
                            refreshLayout.finishLoadMoreWithNoMoreData();
                            return;
                        }
                        if (pn == 1) {
                            datas.clear();
                            refreshLayout.finishRefresh();
                            refreshLayout.setNoMoreData(false);
                            refreshLayout.finishLoadMore();
                        }
                        if (data.size() == 0) {
                            refreshLayout.finishLoadMoreWithNoMoreData();//将不会再次触发加载更多事件
                        } else {
                            refreshLayout.finishLoadMore();
                            pn++;
                        }

                        for (ThumbsDataBean dataBean : data) {

                            dataBean.setOriginal_text(getNotEmu(dataBean.getOriginal_text()));
                            datas.add(new MainBean(dataBean, 1));
                            datas.add(new MainBean(dataBean, 3));
                        }
                        adapter.notifyDataSetChanged();
                        if (datas.size() == 0) {
                            root_content_null_ll.setVisibility(View.VISIBLE);
                            refreshLayout.setVisibility(View.GONE);
                            retryText.setText("暂无数据T T");
                        } else {
                            root_content_null_ll.setVisibility(View.GONE);
                            refreshLayout.setVisibility(View.VISIBLE);

                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        refreshLayout.finishRefresh();
                        refreshLayout.finishLoadMore();
                    }
                });
    }

    private String getNotEmu(String str) {
        if (str.contains("EM-")) {
            try {
                str = StringToUnicodeUtil.unicode2String(str);
                return str;
            } catch (Exception e) {
                LogUtils.e("---m---e.getMessage():" + e.getMessage());
            }
        }
        return str;
    }

    class VHHead extends RecyclerView.ViewHolder {

        SimpleDraweeView userIcon;
        TextView userName;
        TextView reply;
        TextView time;
        View view;

        public VHHead(View itemView) {
            super(itemView);
            userIcon = itemView.findViewById(R.id.userIcon);
            userName = itemView.findViewById(R.id.userName);
            reply = itemView.findViewById(R.id.reply);
            time = itemView.findViewById(R.id.time);
            view = itemView;
        }
    }

    class VHContent extends RecyclerView.ViewHolder {

        TextView content;


        public VHContent(View itemView) {
            super(itemView);
            content = itemView.findViewById(R.id.content);

        }
    }

    class VHOrginal extends RecyclerView.ViewHolder {

        TextView orignal;
        View view;

        public VHOrginal(View itemView) {
            super(itemView);
            view = itemView;
            orignal = itemView.findViewById(R.id.orignal);
        }
    }

    class VHReply extends RecyclerView.ViewHolder {

        TextView replyContent;

        public VHReply(View itemView) {
            super(itemView);
            replyContent = itemView.findViewById(R.id.replyContent);
        }
    }

    class MYThumbsAdapter extends RecyclerView.Adapter {

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            RecyclerView.ViewHolder holder = null;
            View view = null;
            switch (viewType) {
                case 1:
                    view = inflater.inflate(R.layout.notice_comment_list_head_layout, parent, false);
                    holder = new VHHead(view);
                    break;
                case 2:

                    view = inflater.inflate(R.layout.notice_comment_list_content_layout, parent, false);
                    holder = new VHContent(view);
                    break;
                case 3:

                    view = inflater.inflate(R.layout.notice_comment_list_orignal_layout, parent, false);
                    holder = new VHOrginal(view);
                    break;
                case 4:

                    view = inflater.inflate(R.layout.notice_comment_list_reply_layout, parent, false);
                    holder = new VHReply(view);
                    break;
            }
            return holder;
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
            int type = getItemViewType(position);
            final ThumbsDataBean dataBean;
            switch (type) {
                case 1:
                    VHHead vhHead = (VHHead) holder;
                    dataBean = (ThumbsDataBean) datas.get(position).object;
                    vhHead.userIcon.setImageURI(Uri.parse(dataBean.getPraise_user_ico()));
                    vhHead.reply.setText("赞了你");
                    vhHead.userName.setText(dataBean.getPraise_title());
                    long time = dataBean.getPraise_time() * 1000l;
                    if (DateUtils.isToday(time)) {
                        vhHead.time.setText(DateUtils.getTimeHM(time));
                    } else if (DateUtils.isToYear(time)) {
                        vhHead.time.setText(DateUtils.getTimeMDHM(time));
                    } else {
                        vhHead.time.setText(DateUtils.getTimeYMDHM(time));
                    }
//                    vhHead.time.setText(DateUtils.getTimeHM(dataBean.getPraise_time() * 1000));
                    vhHead.view.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            Intent intent = new Intent(v.getContext(), CommentDetailActivity.class);
                            intent.putExtra("commentId", dataBean.getOriginal_text_id() + "");
                            intent.putExtra("gameid", dataBean.getGame_id());
                            startActivity(intent);
                        }
                    });
                    break;
                case 2:
                    VHContent vhContent = (VHContent) holder;

                    break;
                case 3:
                    VHOrginal vhOrginal = (VHOrginal) holder;
                    dataBean = (ThumbsDataBean) datas.get(position).object;
                    vhOrginal.orignal.setText("原文：" + dataBean.getOriginal_text());
                    vhOrginal.view.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            Intent intent = new Intent(v.getContext(), CommentDetailActivity.class);
                            intent.putExtra("commentId", dataBean.getOriginal_text_id() + "");
                            intent.putExtra("gameid", dataBean.getGame_id());
                            startActivity(intent);
                        }
                    });
                    break;
                case 4:
                    VHReply vhReply = (VHReply) holder;

                    break;
            }


        }

        @Override
        public int getItemViewType(int position) {
//            CommentMainBean mainBean=games.get(position);
            return datas.get(position).type;
        }

        @Override
        public int getItemCount() {
            return datas.size();
        }
    }


}
